using System;
using System.Collections.Generic;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;


namespace OhioState.Graphics.OpenGL
{
    public class MotionBlurredLayerHandler : OpenGLObject, ILayer
    {
        static readonly int _frameCount = 6;

        #region ILayer Members
        public bool Active
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set { throw new ApplicationException("Set methods should not be called on Handlers"); }
        }

        public OhioState.Graphics.ICamera Camera
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set { throw new ApplicationException("Set methods should not be called on Handlers"); }
        }

        public List<OhioState.Graphics.IDrawable> Drawables
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public OhioState.Graphics.IMaterial Material
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set { throw new ApplicationException("Set methods should not be called on Handlers"); }
        }

        public IFrameBufferObject FrameBuffer
        {
            get { return _layerProxy.FrameBuffer; }
            set { throw new ApplicationException("Set methods should not be called on Handlers"); }
        }

        //private int _delay = 0;
        //private int _offset = 0;
        private bool _paused = false;
        public bool Paused
        {
            get { return _paused; }
            set { _paused = value; }
        }

        public void Render(IRenderPanel panel)
        {
            _paused = _layerProxy.Paused;
            for (int i = 0; i < _frameCount; i++)
            {
                bool invalidate = (panel.FrameCount % _frameCount) == i;
                invalidate = invalidate & !_paused;
                invalidate = invalidate & _motionLayerHandlers[i].Invalid;
                if (invalidate)
                {
                    _motionLayerHandlers[i].UpdateLayer(panel);
                }
                _motionLayerHandlers[i].Render(panel);
            }
        }

        #endregion
        private List<LayerHandler> _motionLayerHandlers = new List<LayerHandler>(_frameCount);
        private MotionBlurredLayerProxy _layerProxy;
        public MotionBlurredLayerHandler(MotionBlurredLayerProxy proxyObj)
            : base(proxyObj)
        {
            _layerProxy = proxyObj;
            //_realLayer = panel.ResourceManager.GetItem(_layerProxy.Layer) as LayerHandler;
            //for (int i = 0; i < _frameCount; i++)
            //{
            //    LayerProxy layer = new LayerProxy(_layerProxy.Layer.Name + i.ToString());
            //    foreach (OhioState.Graphics.IDrawable drawable in _layerProxy.Layer.Drawables)
            //    {
            //        layer.addDrawableProxy(drawable);
            //    }
            //    layer.Camera = _layerProxy.Layer.Camera;
            //    //layer.Material.CurrentColor.A = 1.0f / 6.0f;
            //    _motionLayerHandlers.Add(new LayerHandler(layer));
            //}
        }
    }
}
